package pt.iscte.pcd.carrace;
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JComponent;

@SuppressWarnings("serial")
public class Track extends JComponent {
	public static final int NUM_STEPS = 100;
	public static final int LENGTH = 500;
	//private static final int NUM_CARS = 10;
	private ImageIcon image_azul = new ImageIcon("azul.gif");
	private ImageIcon image_amarelo = new ImageIcon("amarelo.gif");
	private int[] position = new int[CarGame.NUM_CARS];

	public Track() {
		initCarPosition();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);		
		double deltaX = ((double) (getWidth() - image_azul.getIconWidth()))
				/ NUM_STEPS;
		//System.out.println("deltaX" + deltaX);
		double deltaY = ((double) (getHeight() / (CarGame.NUM_CARS + 1)));
		for (int i = 0; i < CarGame.NUM_CARS; i++) {
			g.drawLine(0,(int)(deltaY*(i+1)) , getWidth(), (int)(deltaY*(i+1)));
			if (i % 2 == 0) 
				g.drawImage(image_azul.getImage(), (int) (deltaX * position[i]),
					(int)(deltaY*(i+1) - image_azul.getIconHeight()), null);
				else
					g.drawImage(image_amarelo.getImage(), (int) (deltaX * position[i]),
							(int)(deltaY*(i+1) - image_amarelo.getIconHeight()), null);
		}
	}

	public void setPosition(int car, int newposition) {
		System.out.println("Car " + car + " progress " + newposition);
		position[car] = newposition;
	}

	private void initCarPosition() {
		for (int i = 0; i < position.length; i++) {
			position[i] = 0;
		}

	}
}
